/*
 * abstract class to a user initiated action, like jump or move. and can be mapped to key with inputmannager
 */

/**
 * @author  8100556811
 */
public class GameAction {

	
	public static final int NORMAL=0;
	public static final int DETECT_INITIAL_PRESS_ONLY=1;
	
	private static final int STATE_RELEASED=0;
	private static final int STATE_PRESSED=1;
	private static final int STATE_WAITING_FOR_RELEASE=2;
	
	/**
	 * @uml.property  name="name"
	 */
	private String name;
	private int behavior;
	/**
	 * @uml.property  name="amount"
	 */
	private int amount;
	private int state;
	
	public GameAction (String name)
	{
		this(name,NORMAL);
	}
	
	public GameAction(String name, int behavior)
	{
		this.name = name;
		this.behavior = behavior;
		reset();
	}
	
	/**
	 * @return
	 * @uml.property  name="name"
	 */
	public String getName()
	{
		return name;
	}
	
	public void reset()
	{
		state = STATE_RELEASED;
		amount=0;
	}
	public synchronized void tap()
	{
		press();
		release();
	}
	
	public synchronized void press()
	{
		press(1);
	}
	
	public synchronized void press(int amount)
	{
		if (state != STATE_WAITING_FOR_RELEASE)
		{
			this.amount+=amount;
			state = STATE_PRESSED;
		}
	}
	
	public synchronized void release()
	{
		state = STATE_RELEASED;
	}
	
	public synchronized boolean isPressed()
	{
		return (getAmount() !=0);
	}
	
	//numer of time the key is pressed
	
	/**
	 * @return
	 * @uml.property  name="amount"
	 */
	public synchronized int getAmount()
	{
		int retVal = amount;
		if (retVal !=0)
		{
			if (state == STATE_RELEASED)
			{
				amount=0;				
			}
			else if (behavior == DETECT_INITIAL_PRESS_ONLY)
			{
				state = STATE_WAITING_FOR_RELEASE;
				amount =0;
			}
		}
		return retVal;
	}
	
	
}
